#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash octopus-11.4.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
if [[ "$1" == "$2" ]]; then echo "构建路径与安装路径不能相同，请输入正确的路径"; exit 1; fi

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
set -e
#yum install -y wget tar perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex environment-modules gcc-c++ git

download_pkg()
{
    wget -t 40 -c $1 -O $buildpath/octopus-11.4.tar.gz
    tar xf $buildpath/octopus-11.4.tar.gz -C $buildpath
    rm -rf $buildpath/octopus-11.4.tar.gz
}

download_pkg https://octopus-code.org/down.php?file=11.4/octopus-11.4.tar.gz
cd $buildpath/octopus-11.4
./configure CC=mpicc CXX=mpicxx FC=mpifort --prefix=$installpath/octopus-11.4 --with-libxc-prefix=$installpath/libxc-4.3.4 --with-libvdwxc-prefix=$installpath/libvdwxc-0.4.0 --with-blas=$installpath/lapack-3.10.1/lib/libblas.a --with-lapack=$installpath/lapack-3.10.1/lib/liblapack.a --with-gsl-prefix=$installpath/gsl-2.5 --with-fftw-prefix=$installpath/fftw-3.3.8 --enable-mpi="-L$installpath/hmpi-1.2.0/hmpi-1.2.0-huawei/lib"
sed -i -e '0,/FINISHED/{s/FINISHED/FINISHED1/}' -e 's/(FINISHED/(FINISHED1/g' src/multisystem/propagator.F90
sed -i 's/type(\*)/type(c_ptr)/g' src/basic/cuda.F90
sed -i '0,/private/{s/private/\!private/}' src/basic/clock.F90
sed -i 's/FINISHED/FINISHED1/g' src/multisystem/system.F90
sed -i -e 's/test_clock_a .lt. test_clock_b/clock_is_earlier(test_clock_a,test_clock_b)/g' \
	-e 's/test_clock_a .le. test_clock_b/clock_is_equal_or_earlier(test_clock_a,test_clock_b)/g' \
	-e 's/test_clock_a .gt. test_clock_b/clock_is_later(test_clock_a,test_clock_b)/g' \
	-e 's/test_clock_a .ge. test_clock_b/clock_is_equal_or_later(test_clock_a,test_clock_b)/g'\
       	-e 's/test_clock_a .eq. test_clock_b/clock_is_equal(test_clock_a,test_clock_b)/g' src/main/test.F90
sed -i '/SAFE_DEALLOCATE_P(systems)/s+^+!+g' src/main/run.F90
sed -i -e 's/cuda_memcpy_htod(this%mem, data(1)/cuda_memcpy_htod(this%mem, c_loc(data(1))/g' \
	-e 's/cuda_memcpy_htod(this%mem, data(1, 1)/cuda_memcpy_htod(this%mem, c_loc(data(1, 1))/g' \
	-e 's/cuda_memcpy_htod(this%mem, data(1, 1, 1)/cuda_memcpy_htod(this%mem, c_loc(data(1, 1, 1))/g' \
	-e 's/cuda_memcpy_dtoh(this%mem, data(1)/cuda_memcpy_dtoh(this%mem, c_loc(data(1))/g' \
	-e 's/cuda_memcpy_dtoh(this%mem, data(1, 1)/cuda_memcpy_dtoh(this%mem, c_loc(data(1, 1))/g' \
	-e 's/cuda_memcpy_dtoh(this%mem, data(1, 1, 1)/cuda_memcpy_dtoh(this%mem, c_loc(data(1, 1, 1))/g' src/basic/accel_inc.F90
make -j$(nproc)
make install

cat>"$installpath/octopus-11.4/octopus_modulefiles"<<EOF
#%Module1.0
conflict octopus

module load $installpath/bisheng-compiler-2.4.0/bisheng_modulefiles
module load $installpath/hmpi-1.2.0/hmpi_modulefiles
module load $installpath/lapack-3.10.1/lapack_modulefiles
module load $installpath/libxc-4.3.4/libxc_modulefiles
module load $installpath/fftw-3.3.8/fftw_modulefiles
module load $installpath/libvdwxc-0.4.0/libvdwxc_modulefiles
module load $installpath/gsl-2.5/gsl_modulefiles
module load $installpath/octopus-11.4/octopus_modulefiles

variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}

set octopus [getModulefileDir]
setenv OCTOPUS \$octopus
prepend-path PATH \$octopus/bin
prepend-path LD_LIBRARY_PATH \$octopus/lib
prepend-path INCLUDE \$octopus/include
EOF

#rm -rf $buildpath/octopus-11.4
